<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * $type
 * 1 - company
 * 2 - recruiment
 * 3 - advertisement
 * 4 - booth
 */

class RateModel extends CI_Model {
    public $table = 'rates';
    public $primary = 'id';
    
    function add($data) {
        $this->db->insert($this->table, $data);
    }
    
    function getRateByUserIdAndRatedId($user_id, $rated_id) {
        $query = $this->db->query("SELECT * FROM {$this->table}
  	                                 WHERE `user_id` = {$user_id}
                                         AND `rated_id` = {$rated_id}");
        return $query->row_array();
    }
    
    function getRateByUserIdAndRatedIdAndType($user_id, $rated_id, $type) {
        $query = $this->db->query("SELECT * FROM {$this->table}
  	                                 WHERE `user_id` = {$user_id}
                                         AND `rated_id` = {$rated_id}
                                         AND `type` = {$type}");
        return $query->row_array();
    }
    
    function getAverageRateByRatedIdAndType($rated_id, $type) {
        $query = $this->db->query("SELECT AVG(`score`) AS `value` FROM {$this->table}
  	                                 WHERE `rated_id` = {$rated_id}
                                         AND `type` = {$type}");
        return $query->row_array();
    }
}
?>
